from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
from fastapi import APIRouter, Depends, HTTPException
from ..core.database import get_db
from ..core.dependencies import get_current_admin_user, get_current_active_user
from ..models.user import User
from ..models.project import Project as ProjectModel
from ..schemas.project import Project, ProjectCreate, ProjectUpdate
from ..services.project_service import ProjectService

router = APIRouter(prefix="/projects", tags=["projects"])

@router.get("/", response_model=List[Project])
def get_projects(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看项目列表
    project_service = ProjectService(db)
    return project_service.get_projects(skip, limit)

@router.post("/", response_model=Project)
def create_project(project: ProjectCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以创建项目
    project_service = ProjectService(db)
    return project_service.create_project(project, current_user.id)

@router.get("/{project_id}", response_model=Project)
def get_project(project_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看项目详情
    project_service = ProjectService(db)
    project = project_service.get_project(project_id)
    if not project:
        raise HTTPException(status_code=404, detail="Project not found")
    return project

@router.put("/{project_id}", response_model=Project)
def update_project(project_id: int, project: ProjectUpdate, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    project_service = ProjectService(db)
    return project_service.update_project(project_id, project)

@router.delete("/{project_id}")
def delete_project(project_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    project_service = ProjectService(db)
    project_service.delete_project(project_id)
    return {"message": "Project deleted successfully"}

@router.get("/status/{status}", response_model=List[Project])
def get_projects_by_status(status: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以按状态查询项目
    project_service = ProjectService(db)
    return project_service.get_project_by_status(status)

@router.get("/stats")
def get_project_stats(db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看项目统计信息
    project_service = ProjectService(db)
    return project_service.get_project_stats()